<template>
  <div v-loading="loading" class="home-container" ref="container" @wheel="wheelHandle">
    <ul
      class="carousel"
      :style="{
        marginTop,
      }"
      @transitionend="transitionEndHandle"
    >
      <li v-for="item in data" :key="item.id">
        <Carouselitem :data="item"/>
      </li>
    </ul>
    <div
      class="icon icon-up"
      v-show="this.index > 0"
      @click="switchTo(index - 1)"
    >
      <Icon type="arrowUp" />
    </div>
    <div
      class="icon icon-down"
      v-show="this.index < data.length - 1"
      @click="switchTo(index + 1)"
    >
      <Icon type="arrowDown" />
    </div>
    <ul class="indicator">
      <li
        v-for="(item, i) in data"
        :key="item.id"
        :class="{
          active: i === index,
        }"
        @click="switchTo(i)"
      ></li>
    </ul>
  </div>
</template>

<script>
import {mapState} from 'vuex';
import Carouselitem from "./Carouselitem.vue";
import Icon from "@/components/Icon/index.vue";
export default {
  components: {
    Carouselitem,
    Icon,
  },
  data() {
    return {
      containerHeight: 0,
      index: 0, //当前页索引
      isSwitching: false, //是否正在翻动
    };
  },
  created(){
    this.$store.dispatch('banner/fetchBanner');
  },
  mounted() {
    this.containerHeight = this.$refs.container.clientHeight;
    window.addEventListener("resize", this.resizeHandle);
  },
  computed: {
    marginTop() {
      return -this.index * this.containerHeight + "px";
    },
    ...mapState('banner',['loading','data']),
  },
  destroyed() {
    window.removeEventListener("resize", this.resizeHandle);
  },
  methods: {
    //传入页码数
    switchTo(i) {
      this.index = i;
    },
    wheelHandle(e) {
      if (this.isSwitching) {
        return;
      }
      if (e.deltaY > 5 && this.index < this.data.length - 1) {
        //如果滚动幅度在这个区间内并且index的值小于数组长度
        this.isSwitching = true;
        this.index++;
      } else if (e.deltaY < -5 && this.index > 0) {
        this.isSwitching = true;
        this.index--;
      }
    },
    transitionEndHandle() {
      this.isSwitching = false;
    },
    resizeHandle() {
      this.containerHeight = this.$refs.container.clientHeight;
    },
  },
};
</script>

<style scoped lang="less">
@import "~@/styles/maxin.less";
@import "~@/styles/var.less";
.home-container {
  width: 100%;
  height: 100%;
  position: relative;
  overflow: hidden;
  ul {
    margin: 0;
    padding: 0;
    list-style: none;
  }
  .carousel {
    width: 100%;
    height: 100%;
    transition: all 0.25s;
    li {
      width: 100%;
      height: 100%;
    }
  }
  .icon {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translateX(-50%);
    font-size: 30px;
    color: @gray;
    cursor: pointer;
    &.icon-up {
      top: 15px;
      animation: arrow-up 1s linear infinite;
    }
    &.icon-down {
      top: auto;
      bottom: 0px;
      animation: arrow-down 1s linear infinite;
    }
  }
  .indicator {
    position: absolute;
    top: 50%;
    left: auto;
    transform: translateY(-50%);
    right: 20px;
    li {
      width: 8px;
      height: 8px;
      border: 1px solid #fff;
      background-color: @words;
      border-radius: 50%;
      margin-bottom: 10px;
      box-sizing: border-box;
      cursor: pointer;
      &.active {
        background-color: #fff;
      }
    }
  }
  @keyframes arrow-up {
    0% {
      transform: translate(-50%, 10px);
    }
    50% {
      transform: translate(-50%, -10px);
    }
    100% {
      transform: translate(-50%, 10px);
    }
  }
  @keyframes arrow-down {
    0% {
      transform: translate(-50%, -10px);
    }
    50% {
      transform: translate(-50%, 10px);
    }
    100% {
      transform: translate(-50%, -10px);
    }
  }
}
</style>